package androidApplication.javaFiles;

import java.util.ArrayList;

import ChatPackage.Chat;
import ChatPackage.ChatHandler;
import Database.Database;
import TomcatRelated.TomcatProtocolImp;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;


public class ChatList extends Activity {


	/**
	 * the list that appears on chat list
	 */
	private Chat _chat;

	/**
	 * the list that appears on chat list
	 */
	private ArrayList<String> _chatList;

	/**
	 * LinearLayout used for maintaining a list of Views that each display Message
	 **/
	private LinearLayout _chatLayout;



	/**
	 * EditText used for entering text for a new Message to be added to chatList.
	 **/
	private EditText _messageEditText;

	/**
	 * Button used for sending and adding a new message to chatList using the
	 * text entered in MessageEditText.
	 **/
	private Button _sendButton;


	/**
	 * Background Color values used for alternating between light and dark rows
	 * of Messages.  
	 */
	private int _nDarkColor;
	private int _nLightColor;

	private String tag = new String("ChatList");

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initLayout();

		//TODO - I canceled the thread!
		Log.v(tag , "starting onCreate()");
		_nDarkColor = R.color.dark;
		_nLightColor = R.color.light;
		initChatList();
		initAddMessageListeners();

	}


	private void initChatList() {
		this._chat = ChatHandler.get_currentChat();
		if(this._chat == null)
			Log.v("testing", "_chat is null");
		this._chatList = this._chat.getMsgList();
		if(this._chatList == null)
			Log.v("testing", "_chatList is null");
		for (String  msg : _chatList) {
			addMessageView(msg);
		}
	}

	/**
	 * Method used to encapsulate the code that initializes and sets the Layout
	 * for this Activity. 
	 */
	protected void initLayout() {

		LinearLayout rootvg = new LinearLayout(this);
		rootvg.setOrientation(LinearLayout.VERTICAL);

		LinearLayout horizonoriented = new LinearLayout(this);
		horizonoriented.setOrientation(LinearLayout.HORIZONTAL);

		_sendButton = new Button(this);
		_sendButton.setText("send message");

		_messageEditText = new EditText(this);
		_messageEditText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT));

		horizonoriented.addView(_sendButton);
		horizonoriented.addView(_messageEditText);

		this._chatLayout = new LinearLayout(this);
		this._chatLayout.setOrientation(LinearLayout.VERTICAL);
		ScrollView sview = new ScrollView(this);
		sview.addView(this._chatLayout);

		rootvg.addView(horizonoriented);
		rootvg.addView(sview);

		setContentView(rootvg);

	}



	/**
	 * Method used to encapsulate the code that initializes and sets the Event
	 * Listeners which will respond to requests to "Add" a new message to the 
	 * list. 
	 */
	protected void initAddMessageListeners() {
		_sendButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String message = _messageEditText.getText().toString();
				_messageEditText.setText("");
				if(!message.equals("")){
					TomcatProtocolImp.getInstance().sendTextMessage
					//TODO - change back to deviceId instead of google_account
					(Database.getInstance(getApplicationContext()).get_device_id(),_chat.getUserAndID(), "Him: "+message);
					_chatList.add("Me: "+message);
					addMessageView("Me: "+message);
				}
			}
		});
	}

	/**
	 * Method used for encapsulating the logic necessary to properly initialize
	 * a new joke, add it to m_arrJokeList, and display it on screen.
	 * 
	 * @param message
	 *            A string containing the text of the message to add.
	 */
	protected void addMessageView(String message) {

		TextView txtView = new TextView(this);
		txtView.setText(message);
		txtView.setTextSize(20);
		txtView.setTextColor(Color.WHITE);
		//txtView.setBackgroundColor(this.getResources().getColor(_nLightColor));

		this._chatLayout.addView(txtView);		
	}

	@Override
	protected void onNewIntent(Intent intent) 
	{
		String message = intent.getStringExtra("message");
		if(message != null && !message.equals(""))
		{
			addReceivedMessage(message);
		}
		else
		{
			super.onNewIntent(intent);
		}
	}


	protected void addReceivedMessage(String message) 
	{
		Log.v("Reuven's", "" + this._chatList);
		this._chatList.add(message);
		addMessageView(message);		
	}


	@Override
	protected void onStop(){
		super.onStop();
		ChatHandler.Set_currentChat(null);
	}

}
